<template>
  <div>
    <el-dialog
      :title="form.id ? '编辑秒杀' : '添加秒杀'"
      :visible.sync="isshow"
      @close="reset"
    >
      <el-form :model="form" :rules="rules" ref="seckillForm">
        <el-form-item
          label="活动名称"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" :label-width="formLabelWidth">
          <el-date-picker
            v-model="date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            @change="getTime"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类" :label-width="formLabelWidth">
          <el-select
            v-model="form.first_cateid"
            @change="select"
            placeholder="请选择一级分类"
          >
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" :label-width="formLabelWidth">
          <el-select
            v-model="form.second_cateid"
            @change="select2"
            placeholder="请选择二级分类"
          >
            <el-option
              v-for="item in secondList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" :label-width="formLabelWidth">
          <el-select v-model="form.goodsid" placeholder="请选择商品">
            <el-option
              v-for="item in goodsList"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reset">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { seckaddedit, seckinfo, goodslist } from "../../request/api";
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      isshow: false,
      formLabelWidth: "120px",
      secondList: [],
      goodsList: [],
      date: [],
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 2,
      },
      rules: {
        title: [
          {
            required: true,
            message: "请输入活动名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    if (this.cateList.length == 0) {
      this.getCateList();
    }
  },
  computed: {
    ...mapState("cate", ["cateList"]),
  },
  methods: {
    ...mapActions("cate", ["getCateList"]),
    ...mapActions("seckill", ["getSeckList"]),
    select(a = true) {
      if (!this.form.id || a) {
        this.form.second_cateid = [];
        this.form.goodsid = [];
      }
      this.secondList = this.cateList.filter(
        (item) => item.id == this.form.first_cateid
      )[0].children;
    },
    select2(a = true) {
      if (!this.form.id || a) {
        this.form.goodsid = [];
      }
      goodslist({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      }).then((res) => {
        this.goodsList = res.list ? res.list : [];
      });
    },
    reset() {
      this.isshow = false;
      this.$refs.seckillForm.resetFields();
      this.date = [];
      this.secondList = [];
      this.goodsList = [];
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 2,
      };
    },
    submitForm() {
      this.$refs.seckillForm.validate((val) => {
        if (!val) {
          return;
        }
        let url = this.form.id ? "/seckedit" : "/seckadd";
        seckaddedit(url, this.form).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            this.getSeckList();
            this.reset();
            return;
          }
          this.$message.error(res.msg);
        });
      });
    },
    getTime(e) {
      if (!e) {
        return;
      }
      if (e.every((item) => item)) {
        this.form.begintime = e[0].getTime();
        this.form.endtime = e[1].getTime();
      }
    },
    getInfo(id) {
      this.isshow = true;
      seckinfo({ id }).then((res) => {
        if (res.code == 200) {
          this.form = res.list;
          this.form.id = id;
          this.select(false);
          this.select2(false);
          this.date = [
            new Date(Number(res.list.begintime)),
            new Date(Number(res.list.endtime)),
          ];
        }
      });
    },
  },
};
</script>

<style scoped>
</style>